package cn.iocoder.yudao.module.wrzj.controller.admin.greatman;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wrzj.controller.admin.greatman.vo.*;
import cn.iocoder.yudao.module.wrzj.dal.dataobject.greatman.GreatmanDO;
import cn.iocoder.yudao.module.wrzj.service.greatman.GreatmanService;

@Tag(name = "管理后台 - 伟人")
@RestController
@RequestMapping("/wrzj/greatman")
@Validated
public class GreatmanController {

    @Resource
    private GreatmanService greatmanService;

    @PostMapping("/create")
    @Operation(summary = "创建伟人")
    @PreAuthorize("@ss.hasPermission('wrzj:greatman:create')")
    public CommonResult<Integer> createGreatman(@Valid @RequestBody GreatmanSaveReqVO createReqVO) {
        return success(greatmanService.createGreatman(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新伟人")
    @PreAuthorize("@ss.hasPermission('wrzj:greatman:update')")
    public CommonResult<Boolean> updateGreatman(@Valid @RequestBody GreatmanSaveReqVO updateReqVO) {
        greatmanService.updateGreatman(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除伟人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wrzj:greatman:delete')")
    public CommonResult<Boolean> deleteGreatman(@RequestParam("id") Integer id) {
        greatmanService.deleteGreatman(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得伟人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wrzj:greatman:query')")
    public CommonResult<GreatmanRespVO> getGreatman(@RequestParam("id") Integer id) {
        GreatmanDO greatman = greatmanService.getGreatman(id);
        return success(BeanUtils.toBean(greatman, GreatmanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得伟人分页")
    @PreAuthorize("@ss.hasPermission('wrzj:greatman:query')")
    public CommonResult<PageResult<GreatmanRespVO>> getGreatmanPage(@Valid GreatmanPageReqVO pageReqVO) {
        PageResult<GreatmanDO> pageResult = greatmanService.getGreatmanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GreatmanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出伟人 Excel")
    @PreAuthorize("@ss.hasPermission('wrzj:greatman:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGreatmanExcel(@Valid GreatmanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GreatmanDO> list = greatmanService.getGreatmanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "伟人.xls", "数据", GreatmanRespVO.class,
                        BeanUtils.toBean(list, GreatmanRespVO.class));
    }

}